package 算法;

/**
 * @author 乐心湖
 * @date 2020/6/19 15:59
 **/
public class ValidPalindrome {
    public boolean isPalindrome1(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                sb.append(c);
            }
        }
        StringBuilder sbReverse = new StringBuilder(sb).reverse();
        return sb.toString().equalsIgnoreCase(sbReverse.toString());
    }

    public boolean isPalindrome2(String s) {
        if (s.isEmpty()) {
            return true;
        }
        s = s.toLowerCase().replaceAll("\\s*", "");

        for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
            while (i < j && !Character.isLetterOrDigit(s.charAt(i))) {
                i++;
            }
            while (i < j && !Character.isLetterOrDigit(s.charAt(j))) {
                j--;
            }
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}
